export function repeatString(string, times = 1) {
    if (times < 0)
        return "";
    if (times === 1)
        return string;
    else
        return string + repeatString(string, times - 1);
}

export function buildVueComputedFromRef(vueRef, propertyName) {
    return Vue.computed({
        get() {
            return vueRef.value[propertyName];
        },
        set(f) {
            vueRef.value[propertyName] = f;
        }
    });
}

export function buildVueComputedListFromRef(vueRef, propertyNameList) {
    const result = [];
    // 一定要用of，js里面，in取出的是下标，of取出的才是元素内容
    for (name of propertyNameList) {
        result.push(buildVueComputedFromRef(vueRef, name));
    }
    return result;
}

export function exist(v) {
    return v !== null && v !== undefined;
}

const appointmentTimeMap = {
    MORNING1: '8:00-10:00',
    MORNING2: '10:00-12:00',
    AFTERNOON1: '13:00-15:00',
    AFTERNOON2: '15:00-17:00',
}

export function appointmentTimeFromType(type) {
    return appointmentTimeMap[type];
}

function dataURLToBlob(dataurl) {
    let arr = dataurl.split(",");
    let mime = arr[0].match(/:(.*?);/)[1];
    let bstr = atob(arr[1]);
    let n = bstr.length;
    let u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], {type: mime});
}

export function doSave(name) {
    return (canvas) => {
        let a = document.createElement("a");
        let dom = document.body.appendChild(canvas);
        dom.style.display = "none";
        a.style.display = "none";
        document.body.removeChild(dom);
        let blob = dataURLToBlob(dom.toDataURL("image/png"));
        a.setAttribute("href", URL.createObjectURL(blob));
        //这块是保存图片操作  可以设置保存的图片的信息
        a.setAttribute("download", name + ".png");
        document.body.appendChild(a);
        a.click();
        URL.revokeObjectURL(blob);
        document.body.removeChild(a);
    };
}

